package com.system.weirdor.www.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.system.weirdor.common.constant.SystemConstant;
import com.system.weirdor.module.entity.User;
import com.system.weirdor.module.service.IUserService;
import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Weridor
 * @since 2017-08-31
 */
@Controller
@RequestMapping("/api/user")
public class UserController  {

@Autowired
private IUserService  userService;
    /**
     * 查询所有用户信息
     *
     * @param pageAble 默认为不分页
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Object queryPage(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam(required = false, defaultValue = "false") boolean pageAble,
                            @RequestParam(required = false, defaultValue = "0") int page,
                            @RequestParam(required = false, defaultValue = "20") int limit) {
        Map<String, Object> condition = new HashMap<String, Object>();
            EntityWrapper<User> wrapper = new EntityWrapper<User>();
            if (!pageAble) {
                condition.put("code",0);
                condition.put("msg","");
                Page<User> pages=userService.selectPage(new Page<User>(page,limit),wrapper);
                condition.put("count",pages.getTotal());
                condition.put("data",pages.getRecords());
            } else {
                condition.put("code",0);
                condition.put("msg","");
                condition.put("count",1000);
                condition.put("data",userService.selectList(wrapper));
            }
            return  condition;
    }

    /**
     * 跳转到用户修改页面
     * @param userId 用户Id
     * @return
     */
    @RequestMapping("/user_update")
    public String userUpdatePage(Model model, Integer userId){
        User user = userService.selectById(userId);
        //修改页面标识
        model.addAttribute("pageFlag", "updatePage");
        model.addAttribute("user", user);
        return "pages/module/user/user_edit";
    }


}
